//
//  HMVisitorView.swift
//  WeiBo
//
//  Created by Feng on 2017/6/5.
//  Copyright © 2017年 feng. All rights reserved.
//

import UIKit
import SnapKit

class HMVisitorView: UIView {

    //定义回调的闭包
    var callBack: (() -> ())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
        
//        setAnimation()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置界面
    private func setupUI()
    {
        backgroundColor = UIColor(white: 234 / 255, alpha: 1)
        
        addSubview(cycleImageView)
        addSubview(maskImageView)
        addSubview(houseImageView)
        addSubview(messageLabel)
        addSubview(loginButton)
        addSubview(registerButton)
        
        //设置约束
        cycleImageView.snp.makeConstraints { (make) in
            
            make.center.equalTo(self)
        }
        
        maskImageView.snp.makeConstraints { (make) in
            make.center.equalTo(cycleImageView)
        }
        
        houseImageView.snp.makeConstraints { (make) in
            make.center.equalTo(cycleImageView)
        }
        
        messageLabel.snp.makeConstraints { (make) in
            make.top.equalTo(maskImageView.snp.bottom)
            make.centerX.equalTo(maskImageView)
            make.width.equalTo(224)
        }
        
        loginButton.snp.makeConstraints { (make) in
            make.top.equalTo(messageLabel.snp.bottom)
            make.leading.equalTo(messageLabel)
            make.width.equalTo(100)
            make.height.equalTo(40)
        }
        
        registerButton.snp.makeConstraints { (make) in
            make.top.equalTo(messageLabel.snp.bottom)
            make.trailing.equalTo(messageLabel)
            make.width.equalTo(100)
            make.height.equalTo(40)
        }
    }

    //设置动画
    func setAnimation()
    {
        //创建基本动画
        let animation: CABasicAnimation = CABasicAnimation()
        //设置动画属性
        animation.keyPath = "transform.rotation"
        //设置弧度
        animation.toValue = Double.pi * 2
        //设置动画时间
        animation.duration = 20
        //动画次数
        animation.repeatCount = MAXFLOAT
        //动画结束,不会从layer上移除动画
        animation.isRemovedOnCompletion = false
        //添加动画
        cycleImageView.layer.add(animation, forKey: nil)
    }
    
    //方法: 根据传入的参数显示不同的图片和label
    func updateInfoView(imageName: String?, labelText: String?)
    {
        if let imgName = imageName,let labText = labelText
        {
            houseImageView.image = UIImage(named: imgName)
            messageLabel.text = labText
            cycleImageView.isHidden = true
        }
        else
        {
            setAnimation()
        }
    }
    
    //设置转圈的imageView
    private lazy var cycleImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    //设置遮盖view
    private lazy var maskImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    //设置房子
    private lazy var houseImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    //设置文字
    private lazy var messageLabel: UILabel =
    {
        let label = UILabel()
        
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        label.numberOfLines = 0
        label.textAlignment = .center
        label.font = UIFont.systemFont(ofSize: 13)
        
        return label
    }()
    
    //设置登录和注册按钮
    private lazy var loginButton: UIButton =
    {
        let button = UIButton()
        
        //设置拉伸图片
        //第一种方法
        let image = UIImage(named: "common_button_white_disable")
//        let stretchImage = image!.stretchableImage(withLeftCapWidth: 5, topCapHeight: 5)
        //第二种方法
        let stretchImage = image!.resizableImage(withCapInsets: UIEdgeInsetsMake(5, 5, 5, 5), resizingMode: UIImageResizingMode.stretch)
        
        button.setTitle("登录", for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        button.setTitleColor(UIColor.lightGray, for: .normal)
        button.setTitleColor(UIColor.orange, for: .highlighted)
        button.setBackgroundImage(stretchImage, for: .normal)
        button.adjustsImageWhenHighlighted = false
        
        //注册点击事件
        button.addTarget(self, action: #selector(loginButtonClick), for: .touchUpInside)
        
        return button
    }()
    
    //设置登录和注册按钮
    private lazy var registerButton: UIButton =
    {
        let button = UIButton()
        
        //设置拉伸图片
        //第一种方法
        let image = UIImage(named: "common_button_white_disable")
        //        let stretchImage = image!.stretchableImage(withLeftCapWidth: 5, topCapHeight: 5)
        //第二种方法
        let stretchImage = image!.resizableImage(withCapInsets: UIEdgeInsetsMake(5, 5, 5, 5), resizingMode: UIImageResizingMode.stretch)
        
        button.setTitle("注册", for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        button.setTitleColor(UIColor.lightGray, for: .normal)
        button.setTitleColor(UIColor.orange, for: .highlighted)
        button.setBackgroundImage(stretchImage, for: .normal)
        button.adjustsImageWhenHighlighted = false
        
        //注册点击事件
        button.addTarget(self, action: #selector(registerButtonClick), for: .touchUpInside)
        
        return button
    }()
    
    func loginButtonClick()
    {
        callBack?()
    }
    
    func registerButtonClick()
    {
        callBack?()
    }
    
}
